﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Project_ScansiaPacific
{
    public partial class FrQLNhapKho : DevComponents.DotNetBar.Office2007Form
    {
        public FrQLNhapKho()
        {
            InitializeComponent();
        }
        private string find;
        public void Search()
        {
            try
            {
                Data dta = new Data();
                DataSet ds = new DataSet();
                string date_start = dateTimePicker1.Value.ToString("MM/dd/yyyy");
                string date_end = dateTimePicker2.Value.ToString("MM/dd/yyyy");
                if (rd_ChungTu.Checked == true)
                {
                   
                    if (txt_Seach.Text == "")
                    {
                        find = @"select  ChungTu, NguonGoc, TKHaiQuan, HDNgay, LoaiGo, LyDoNhap, ADorKD, FCS, NhapTu, LoSay, TinhTrangGo, NgayRaLo, NgayVaoLo, NgayNhap, Serial_NK
                                    from Serial_NKGo";
                    }
                    else
                        find = @"select  ChungTu, NguonGoc, TKHaiQuan, HDNgay, LoaiGo, LyDoNhap, ADorKD, FCS, NhapTu, LoSay, TinhTrangGo, NgayRaLo, NgayVaoLo, NgayNhap, Serial_NK
                                    from Serial_NKGo
                                    where ChungTu like '" + txt_Seach.Text + "%'";
                }
                if (rd_NguonCungCap.Checked == true)
                {
                    if (txt_Seach.Text != "")
                    {
                        find = @"select  ChungTu, NguonGoc, TKHaiQuan, HDNgay, LoaiGo, LyDoNhap, ADorKD, FCS, NhapTu, LoSay, TinhTrangGo, NgayRaLo, NgayVaoLo, NgayNhap, Serial_NK
                                from Serial_NKGo
                                where NhapTu like'" + txt_Seach.Text + "%' and NgayNhap>='" + date_start + " 00:00:00' and NgayNhap<='" + date_end + " 23:59:59'";
                    }
                    if (txt_Seach.Text == "")
                    {
                        find = @"select  ChungTu, NguonGoc, TKHaiQuan, HDNgay, LoaiGo, LyDoNhap, ADorKD, FCS, NhapTu, LoSay, TinhTrangGo, NgayRaLo, NgayVaoLo, NgayNhap, Serial_NK
                                from Serial_NKGo
                                where NgayNhap>='" + date_start + " 00:00:00' and NgayNhap<='" + date_end + " 23:59:59'";
                    }
                }
                ds = dta.dts(find);
                dataGridView1.DataSource = ds.Tables[0];
                ds.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void FrQLNhapKho_Load(object sender, EventArgs e)
        {
            dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = Color.Red;
            dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Yellow;
            dataGridView1.EnableHeadersVisualStyles = false;

        }
        public void User(string s)
        {
            lb_User.Text = s;
        }

        private void bt_search_Click(object sender, EventArgs e)
        {
            Search();
        }

        private void rd_ChungTu_CheckedChanged(object sender, EventArgs e)
        {
            lb.Text = "Chứng Từ";
            dateTimePicker1.Enabled = false;
            dateTimePicker2.Enabled = false;
        }

        private void rd_NguonCungCap_CheckedChanged(object sender, EventArgs e)
        {
            lb.Text = "Nguồn Cung Cấp";
            dateTimePicker1.Enabled = true;
            dateTimePicker2.Enabled = true;
        }

        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                Process prs = new Process();
                FrEditNK FrE = new FrEditNK();
                int r = dataGridView1.CurrentCell.RowIndex;
                int index_ = int.Parse(dataGridView1.Rows[r].Cells["Serial_NK"].Value.ToString());
                string sql_select = @"select Serial_NK,ChungTu,NguonCungCap,ChieuDay,ChieuRong,ChieuDai,SoLuong,SoKhoi,GhiChu,STT from dbo.NhapKhoGo
                                       where Serial_NK=" + index_;
                
                
                DataSet ds = prs.dts(sql_select);
                //MessageBox.Show(sql_select);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    FrE.Show();
                    FrE.serial(index_.ToString());
                    FrE.datagrid_load(ds);
                    FrE.User(lb_User.Text,lb_User.Text);
                    FrE.ChungTu(ds.Tables[0].Rows[0]["ChungTu"].ToString());
                    ds.Dispose();
                }
                else
                    MessageBox.Show("Chứng từ này không có hàng hóa nào");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void report()
        {
            Fr_Report FrR = new Fr_Report();
            //int r = dataGridView1.CurrentCell.RowIndex;
            //string select;
            //string NhapTu1 = dataGridView1.Rows[r].Cells["NhapTu"].Value.ToString();
            try
            {
                Data dta = new Data();
                DataSet ds = new DataSet();
                string date_start = dateTimePicker1.Value.ToString("MM/dd/yyyy");
                string date_end = dateTimePicker2.Value.ToString("MM/dd/yyyy");
                if (rd_ChungTu.Checked == true)
                {

                    if (txt_Seach.Text == "")
                    {
                        find = @"select  Seri.NhapTu as TenNCC,NhaCungCap.TenNCC as TenSP,Seri.ChungTu,Seri.NgayNhap,Seri.LyDoNhap,Seri.TinhTrangGo,Seri.ADorKD,Seri.LoaiGo, Seri.Serial_NK,Nhap.ChieuDay as ChieuRong,Nhap.ChieuRong as ChieuDai,Nhap.ChieuDai as ChieuCao,Nhap.SoLuong,Nhap.SoKhoi
                                from Serial_NKGo as Seri inner join dbo.NhapKhoGo as Nhap on Seri.Serial_NK=Nhap.Serial_NK inner join dbo.NhaCungCap on Seri.NhapTu=NhaCungCap.MaNCC";
                    }
                    else
                        find = @"
                                 select  Seri.NhapTu as TenNCC,NhaCungCap.TenNCC as TenSP,Seri.ChungTu,Seri.NgayNhap,Seri.LyDoNhap,Seri.TinhTrangGo,Seri.ADorKD,Seri.LoaiGo, Seri.Serial_NK,Nhap.ChieuDay as ChieuRong,Nhap.ChieuRong as ChieuDai,Nhap.ChieuDai as ChieuCao,Nhap.SoLuong,Nhap.SoKhoi
                                from Serial_NKGo as Seri inner join dbo.NhapKhoGo as Nhap on Seri.Serial_NK=Nhap.Serial_NK inner join dbo.NhaCungCap on Seri.NhapTu=NhaCungCap.MaNCC
                                    where Seri.ChungTu like '" + txt_Seach.Text + "%'";
                }
                if (rd_NguonCungCap.Checked == true)
                {
                    if (txt_Seach.Text != "")
                    {
                        find = @"select  Seri.NhapTu as TenNCC,NhaCungCap.TenNCC as TenSP,Seri.ChungTu,Seri.NgayNhap,Seri.LyDoNhap,Seri.TinhTrangGo,Seri.ADorKD,Seri.LoaiGo, Seri.Serial_NK,Nhap.ChieuDay as ChieuRong,Nhap.ChieuRong as ChieuDai,Nhap.ChieuDai as ChieuCao,Nhap.SoLuong,Nhap.SoKhoi
                                from Serial_NKGo as Seri inner join dbo.NhapKhoGo as Nhap on Seri.Serial_NK=Nhap.Serial_NK inner join dbo.NhaCungCap on Seri.NhapTu=NhaCungCap.MaNCC
                                where NhapTu like'" + txt_Seach.Text + "%' and NgayNhap>='" + date_start + " 00:00:00' and NgayNhap<='" + date_end + " 23:59:59'";
                    }
                    if (txt_Seach.Text == "")
                    {
                        find = @"select  Seri.NhapTu as TenNCC,NhaCungCap.TenNCC as TenSP,Seri.ChungTu,Seri.NgayNhap,Seri.LyDoNhap,Seri.TinhTrangGo,Seri.ADorKD,Seri.LoaiGo, Seri.Serial_NK,Nhap.ChieuDay as ChieuRong,Nhap.ChieuRong as ChieuDai,Nhap.ChieuDai as ChieuCao,Nhap.SoLuong,Nhap.SoKhoi
                                from Serial_NKGo as Seri inner join dbo.NhapKhoGo as Nhap on Seri.Serial_NK=Nhap.Serial_NK inner join dbo.NhaCungCap on Seri.NhapTu=NhaCungCap.MaNCC
                                where NgayNhap>='" + date_start + " 00:00:00' and NgayNhap<='" + date_end + " 23:59:59'";
                    }
                }
                ds = dta.dts(find);
                FrR.report_QLNKXK(ds);
                FrR.Show();
                ds.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }    

        }

        private void bt_report_Click(object sender, EventArgs e)
        {
            report();
        }

    }
}
